package com.itheima.leetcode.od.c.bf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * <h3>用连续自然数之和来表达整数</h3>
 */
public class SumOfConsecutiveNaturalNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        getResult(sc.nextInt());
    }

    public static void getResult(int t) {
        int[] arr = new int[t];
        for (int i = 0; i < t; i++) {
            arr[i] = i + 1;
        }

        ArrayList<int[]> ans = new ArrayList<>();

        int l = 0;
        int r = 1;

        int sum = arr[l];
        while (l < t) { // 前闭后开
            if (sum > t) {
                sum -= arr[l++];
            } else if (sum == t) {
                ans.add(Arrays.copyOfRange(arr, l, r));
                sum -= arr[l++];
                if (r >= t) { // 减枝 因为到了极限
                    break;
                }
                sum += arr[r++];
            } else {
                sum += arr[r++];
            }
        }

        System.out.println(ans.stream()
                .sorted((a, b) -> a.length - b.length)
                .map(ints -> Arrays.stream(ints)
                        .boxed()
                        .map(String::valueOf)
                        .collect(Collectors.joining("+", t + "=", "")))
                .collect(Collectors.joining("\n")));

        System.out.println("Result:" + ans.size());
    }
}